/**
 * 
 */
package com.mzba.place.utils;

import java.io.IOException;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.net.RequestListener;

/**
 * @author iris
 *
 */
public class ShareUtils {
	
	private static Handler handler;
	private static int what;
	
	/**
	 * 发送致微信或者朋友网
	 * @param urls 地址
	 * @param text 文本
	 * @param bmp  图片
	 * @param type  微信或者朋友网
	 * @param menu_weixin_friend 朋友网
	 * @param api  微信api
	 */
	public static void sendWeixinAndFriend(String urls, String text, Bitmap bmp, int type,int menu_weixin_friend,IWXAPI api) {

		String url = urls; // 收到分享的好友点击信息会跳转到这个地址去
		WXWebpageObject localWXWebpageObject = new WXWebpageObject();
		localWXWebpageObject.webpageUrl = url;
		WXMediaMessage localWXMediaMessage = new WXMediaMessage(
				localWXWebpageObject);
		localWXMediaMessage.title = "身边安全,一目了然";// 不能太长，否则微信会提示出错。不过博主没验证过具体能输入多长。
		localWXMediaMessage.description = text;

		localWXMediaMessage.thumbData = Utils.bmpToByteArray(bmp, true);
		SendMessageToWX.Req localReq = new SendMessageToWX.Req();
		localReq.transaction = System.currentTimeMillis() + "";
		localReq.message = localWXMediaMessage;
		// localReq.scene = isTimelineCb.isChecked() ?
		// SendMessageToWX.Req.WXSceneTimeline :
		// SendMessageToWX.Req.WXSceneSession;
		if (type == menu_weixin_friend) {
			localReq.scene = SendMessageToWX.Req.WXSceneTimeline;
		} else {
			localReq.scene = SendMessageToWX.Req.WXSceneSession;
		}

		api.sendReq(localReq);
	}
	
	
	
	/**
	 * 新浪微博返回信息
	 */
	public static RequestListener sinaRequestListener = new RequestListener() {
		@Override
		public void onIOException(IOException arg0) {
			Log.d("debug", "onIOException---===>" + arg0.getMessage());
		}

		@Override
		public void onError(WeiboException arg0) {
			Log.d("debug", "onError---===>" + arg0.getMessage());
		}

		@Override
		public void onComplete(String arg0) {
			// TODO Auto-generated method stub
			Log.d("debug", "onComplete");
			handler.sendEmptyMessage(what);
		}
	};
	
	public static void setHandle(Handler handler,int what){
		ShareUtils.handler = handler;
		ShareUtils.what = what;
	}
	
	public static class BaseUiListener implements IUiListener {

		@Override
		public void onComplete(JSONObject response) {
			Log.d("debug", "doComplete---==>code:" + response.toString());
			doComplete(response);
		}

		protected void doComplete(JSONObject values) {

		}

		@Override
		public void onError(UiError e) {
			Log.d("debug", "UiError---===>" + e.errorCode + ", msg:"
					+ e.errorMessage + ", detail:" + e.errorDetail);
			
		}

		@Override
		public void onCancel() {
			Log.d("debug", "onCancel---===>");
		}
	}
}
